package work803.work1;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Service {
  private static List<Student> studentList = new ArrayList<>();
  static {
    studentList.add(new Student(1,19,"小明","111111111111111111"));
    studentList.add(new Student(2,20,"小王","111161651611111111"));
    studentList.add(new Student(3,21,"小刚","111111111164984841"));
    studentList.add(new Student(4,22,"小智","119498804981361611"));
  }

  /**
   * 增加新成员
   * @param student
   * @return
   */
  public static boolean addStudent(Student student){
    if (studentList.contains(student)){
      throw new RuntimeException("学号重复！");
    }
    studentList.add(student);
    return true;
  }

  /**
   * 根据学号删除学生
   * @param studentId
   * @return
   */
  public static boolean delStudent(int studentId){
    Iterator<Student> studentIterator = studentList.iterator();
    while (studentIterator.hasNext()){
      Student student = studentIterator.next();
      if (student.getId() == studentId){
        studentIterator.remove();
        return true;
      }
    }
    return false;
  }

  /**
   * 根据学号修改信息
   * @param student
   * @return
   */
  public static boolean updateStudent(Student student){
    int a = -1;
    for (int i = 0; i <studentList.size() ; i++) {

      if (student.getId() == studentList.get(i).getId()){
        a = i;
        break;
      }
    }
    if (a != -1){
      studentList.set(a,student);
      return true;
    }
    return false;
  }

  /**
   * 插看全体成员
   * @return
   */
  public static List<Student> selectAll(){
    List<Student> studentList1 = new ArrayList<Student>();
    for (int i = 0; i < studentList.size(); i++) {
      if (studentList.get(i) != null){
        studentList1.add(studentList.get(i));
      }
    }
    return studentList1;
  }
}
